﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using RN.ERP.Web.OrderService;
using Newtonsoft.Json;
using RN.ERP.Web.ITrusRAService;

namespace RN.ERP.Web.CA
{
    public partial class CAUpdate : System.Web.UI.Page
    {
        public string CASubId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidId.Value = Request.QueryString["id"];
            }
        }

        [WebMethod]
        public static string ValidateKey(object dogid, object subid, object name)
        {
            int dogId;
            int subId;
            string pro = null;
            if (int.TryParse(dogid.ToString(), out dogId) && int.TryParse(subid.ToString(), out subId))
            {
                OrderServiceClient client = new OrderServiceClient();
                ModelService.OperationResult result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.ValidateKeyStatus(subId, dogId, name.ToString()));
                pro = JsonConvert.SerializeObject(new { value = result.Result, mes = result.Message });
            }
            else
            {
                pro = JsonConvert.SerializeObject(new { value = false, mes = "验证失败" });
            }
            return pro;
        }

        [WebMethod]
        public static string UpdateCert(object csr,
            object csrSigned,
            object certSN,
            object cert,
            object entity,
            object count,
            object raEntity,
            object KeySN,
            object EntityGuid,
            object EntityName,
            object subid)
        {
            string pro = null;
            ITrusRAServiceClient iTrusClient = new ITrusRAServiceClient();
            string operation = iTrusClient.RaRenewCert(bool.Parse(raEntity.ToString()), int.Parse(count.ToString()),
                csr.ToString(), csrSigned.ToString(), cert.ToString(), certSN.ToString());
            if (operation.Contains("$@"))
            {
                pro = JsonConvert.SerializeObject(new { value = "-1", mes = operation.TrimStart('$').TrimStart('@') });
            }
            else
            {
                OrderServiceClient client = new OrderServiceClient();
                ModelService.OperationResult result = JsonConvert.DeserializeObject<ModelService.OperationResult>
                    (client.UpdateCert(int.Parse(subid.ToString()), entity.ToString(), Guid.Parse(EntityGuid.ToString()), EntityName.ToString(),
                    KeySN.ToString(), operation));
                pro = JsonConvert.SerializeObject(new { value = result.Message.Split('|')[0], mes = result.Message });
            }
            return pro;
        }
    }
}